﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KCMS.DataAcsess.Interfaces;
using StructureMap;
using AutoMapper;
using KCMS.BusinessLogic.Models;
using KCMS.BusinessLogic.Interfaces;


namespace KCMS.BusinessLogic.Engine
{
    public class ClientService : IClientService
    {
        private IClientManager clientManager = ObjectFactory.GetInstance<IClientManager>();
        public Models.Client getById(Guid id)
        {
            DataAcsess.Entitys.ClientEntity clientEntity = clientManager.getById(id);
            return Mapper.Map<DataAcsess.Entitys.ClientEntity, Models.Client>(clientEntity);
        }

        public IEnumerable<Models.Client> getAll()
        {
            var model = from t in clientManager.getAll()
                        select t;
            return Mapper.Map<IEnumerable<DataAcsess.Entitys.ClientEntity>, IEnumerable<Models.Client>>(model);

        }

        public void update(Client client)
        {
            clientManager.update(Mapper.Map<Client, DataAcsess.Entitys.ClientEntity>(client));
        }


        public Guid save(Client client)
        {
            var test = (from t in getAll()
                        where t.Email.Equals(client.Email)
                        select t).SingleOrDefault();
            if (test != null)
            {
                return test.Id;
            }
            else
            {
                return clientManager.save(Mapper.Map<Client, DataAcsess.Entitys.ClientEntity>(client));
            }
        }


        public void delete(Guid id)
        {
            clientManager.delete(id);
        }
    }
}
